<template>
  <div class="app-container">
    <el-tabs :tab-position="tabPosition" style="height: 800px;">
      <el-tab-pane label="工单管理">
        <el-card>
          工单管理
        </el-card>
        <el-card>
          <el-form :model="queryParams_GZD" ref="queryForm_GZD" size="small" :inline="true" v-show="showSearch_GZD"
                   label-width="100px">
            <el-form-item label="工单编号" prop="workOrderId">
              <el-input
                v-model="queryParams_GZD.workOrderId"
                placeholder="请输入工单编号"
                clearable
                @keyup.enter.native="handleQuery_GZD"
              />
            </el-form-item>
            <el-form-item label="客户编号" prop="customerId">
              <el-input
                v-model="queryParams_GZD.customerId"
                placeholder="请输入客户编号"
                clearable
                @keyup.enter.native="handleQuery_GZD"
              />
            </el-form-item>

            <el-form-item label="车辆入厂时间" prop="vehicleEntryTime">
              <el-date-picker
                v-model="queryParams_GZD.vehicleEntryTime"
                align="right"
                type="date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="预计完成时间" prop="vehicleEntryTime">
              <el-date-picker
                v-model="queryParams_GZD.estimatedCompletionTime"
                align="right"
                type="date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="维修状态" prop="repairStatus">
              <el-select v-model="queryParams_GZD.repairStatus" placeholder="请选择">
                <el-option
                  v-for="item in repairStatusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery_GZD">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery_GZD">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                disabled
                plain
                size="mini"
                @click="handleAdd_GZD"
                v-hasPermi="['system:work_order_info:add']"
              >系统自动生成
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="singleGZD"
                @click="handleUpdate_GZD"
                v-hasPermi="['system:work_order_info:edit']"
              >修改
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multipleGZD"
                @click="handleDelete_GZD"
                v-hasPermi="['system:work_order_info:remove']"
              >删除
              </el-button>
            </el-col>
            <right-toolbar :showSearch_GZD.sync="showSearch_GZD" @queryTable="getListGZD"></right-toolbar>
          </el-row>

          <el-table v-loading="loading_GZD" :data="work_order_infoList" @selection-change="handleSelectionChangeGZD">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="工单编号" align="center" prop="workOrderId"/>
            <el-table-column label="客户编号" align="center" prop="customerId"/>
            <el-table-column label="车辆入厂时间" align="center" prop="vehicleEntryTime"/>
            <el-table-column label="预计完成时间" align="center" prop="estimatedCompletionTime"/>
            <el-table-column label="类型" align="center" prop="repairType"/>
            <el-table-column label="维修内容" align="center" prop="repairContent"/>
            <el-table-column label="维修状态" align="center" prop="repairStatus"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate_GZD(scope.row)"
                  v-hasPermi="['system:work_order_info:edit']"
                >修改
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete_GZD(scope.row)"
                  v-hasPermi="['system:work_order_info:remove']"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="totalGZD>0"
            :total="totalGZD"
            :page-sizes=pageSizes
            :page.sync="queryParams_GZD.pageNum"
            :limit.sync="queryParams_GZD.pageSize"
            @pagination="getListGZD"
          />

          <!-- 添加或修改工作单信息对话框 -->
          <el-dialog :title="titleGZD" :visible.sync="openGZD" width="800px" append-to-body>
            <el-form ref="formGZD" :model="formGZD" :rules="rulesGZD" label-width="80px">
              <el-form-item label="工单编号" prop="workOrderId">
                <el-input v-model="formGZD.workOrderId" placeholder="请输入工单编号" disabled/>
              </el-form-item>
              <el-form-item label="客户编号" prop="customerId">
                <el-input v-model="formGZD.customerId" placeholder="请输入客户编号" disabled/>
              </el-form-item>
              <el-form-item label="车辆入厂时间" prop="vehicleEntryTime">
                <el-input v-model="formGZD.vehicleEntryTime" placeholder="请输入车辆入厂时间" disabled/>
              </el-form-item>
              <el-form-item label="维修状态" prop="repairStatus">
                <el-select v-model="formGZD.repairStatus" placeholder="请选择">
                  <el-option
                    v-for="item in repairStatusOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="预计完成时间" prop="estimatedCompletionTime">
                <el-date-picker
                  v-model="formGZD.estimatedCompletionTime"
                  align="right"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期"
                >
                </el-date-picker>
              </el-form-item>

              <el-form-item label="维修内容">
                <editor v-model="formGZD.repairContent" :min-height="192"/>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitFormGZD">确 定</el-button>
              <el-button @click="cancelGZD">取 消</el-button>
            </div>
          </el-dialog>
        </el-card>
      </el-tab-pane>
      <el-tab-pane label="维修管理">
        <el-card>
          维修管理
        </el-card>
        <el-card>
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                   label-width="100px">
            <el-form-item label="维修单编号" prop="maintenanceOrderId">
              <el-input
                v-model="queryParams.maintenanceOrderId"
                placeholder="请输入维修单编号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="工单编号" prop="workOrderId">
              <el-input
                v-model="queryParams.workOrderId"
                placeholder="请输入工单编号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="客户编号" prop="customerId">
              <el-input
                v-model="queryParams.customerId"
                placeholder="请输入客户编号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="使用配件编号" prop="sparePartsId">
              <el-input
                v-model="queryParams.sparePartsId"
                placeholder="请输入使用配件编号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="维修费用" prop="maintenanceCost">
              <el-input
                v-model="queryParams.maintenanceCost"
                placeholder="请输入维修费用"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>

            <el-form-item label="是否支付" prop="isPaid">
              <el-select v-model="queryParams.isPaid" placeholder="请选择">
                <el-option
                  v-for="item in isPaidOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                disabled
                plain
                size="mini"
                @click="handleAdd"
                v-hasPermi="['system:maintenance_order_info:add']"
              >每5s自动更新，或点击最右侧刷新按钮手动更新
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['system:maintenance_order_info:edit']"
              >修改
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['system:maintenance_order_info:remove']"
              >删除
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                v-hasPermi="['system:maintenance_order_info:export']"
              >导出
              </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="maintenance_order_infoList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="维修单编号" align="center" prop="maintenanceOrderId"/>
            <el-table-column label="工单编号" align="center" prop="workOrderId"/>
            <el-table-column label="客户编号" align="center" prop="customerId"/>
            <el-table-column label="维修状态" align="center" prop="repairStatus"/>
            <el-table-column label="使用配件编号" align="center" prop="sparePartsId"/>
            <el-table-column label="维修费用" align="center" prop="maintenanceCost"/>
            <el-table-column label="是否支付" align="center" prop="isPaid"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:maintenance_order_info:edit']"
                >修改
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['system:maintenance_order_info:remove']"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page-sizes=pageSizes
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />

          <!-- 添加或修改维修单信息对话框 -->
          <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
              <el-form-item label="维修单编号" prop="maintenanceOrderId">
                <el-input v-model="form.maintenanceOrderId" placeholder="请输入维修单编号"/>
              </el-form-item>
              <el-form-item label="工单编号" prop="workOrderId">
                <el-input v-model="form.workOrderId" placeholder="请输入工单编号"/>
              </el-form-item>
              <el-form-item label="客户编号" prop="customerId">
                <el-input v-model="form.customerId" placeholder="请输入客户编号"/>
              </el-form-item>
              <el-form-item label="使用配件编号" prop="sparePartsId">
                <el-input v-model="form.sparePartsId" placeholder="请输入使用配件编号"/>
              </el-form-item>
              <el-form-item label="维修费用" prop="maintenanceCost">
                <el-input v-model="form.maintenanceCost" placeholder="请输入维修费用"/>
              </el-form-item>
<!--              <el-form-item label="是否支付" prop="isPaid">-->
<!--                <el-input v-model="form.isPaid" placeholder="请输入是否支付"/>-->
<!--              </el-form-item>-->
              <el-form-item label="是否支付" prop="isPaid">
                <el-select v-model="form.isPaid" placeholder="请选择">
                  <el-option
                    v-for="item in isPaidOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
            </div>
          </el-dialog>
        </el-card>
      </el-tab-pane>
      <el-tab-pane label="配件管理">
        <el-card>
          配件管理
        </el-card>
        <el-card>
          <el-form :model="queryParams_PeiJian" ref="queryForm_peijian" size="small" :inline="true"
                   v-show="showSearch_peijian" label-width="68px">
            <el-form-item label="配件编号" prop="accessoryId">
              <el-input
                v-model="queryParams_PeiJian.accessoryId"
                placeholder="请输入配件编号"
                clearable
                @keyup.enter.native="handleQuery_peijian"
              />
            </el-form-item>
            <el-form-item label="配件名称" prop="accessoryName">
              <el-input
                v-model="queryParams_PeiJian.accessoryName"
                placeholder="请输入配件名称"
                clearable
                @keyup.enter.native="handleQuery_peijian"
              />
            </el-form-item>
            <el-form-item label="入库量" prop="stockInput">
              <el-input
                v-model="queryParams_PeiJian.stockInput"
                placeholder="请输入入库量"
                clearable
                @keyup.enter.native="handleQuery_peijian"
              />
            </el-form-item>
            <el-form-item label="出库量" prop="stockOutput">
              <el-input
                v-model="queryParams_PeiJian.stockOutput"
                placeholder="请输入出库量"
                clearable
                @keyup.enter.native="handleQuery_peijian"
              />
            </el-form-item>
            <el-form-item label="库存量" prop="stockQuantity">
              <el-input
                v-model="queryParams_PeiJian.stockQuantity"
                placeholder="请输入库存量"
                clearable
                @keyup.enter.native="handleQuery_peijian"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery_peijian">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery_peijian">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd_peijian"
                v-hasPermi="['system:accessory_info:add']"
              >新增
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single_peijian"
                @click="handleUpdate_peijian"
                v-hasPermi="['system:accessory_info:edit']"
              >修改
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple_peijian"
                @click="handleDelete_peijian"
                v-hasPermi="['system:accessory_info:remove']"
              >删除
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                v-hasPermi="['system:accessory_info:export']"
              >导出
              </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch_peijian" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading_peijian" :data="accessory_infoList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="配件编号" align="center" prop="accessoryId"/>
            <el-table-column label="配件名称" align="center" prop="accessoryName"/>
            <el-table-column label="入库量" align="center" prop="stockInput"/>
            <el-table-column label="出库量" align="center" prop="stockOutput"/>
            <el-table-column label="库存量" align="center" prop="stockQuantity"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate_peijian(scope.row)"
                  v-hasPermi="['system:accessory_info:edit']"
                >修改
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete_peijian(scope.row)"
                  v-hasPermi="['system:accessory_info:remove']"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total_peijian>0"
            :total="total_peijian"
            :page-sizes=pageSizes
            :page.sync="queryParams_PeiJian.pageNum"
            :limit.sync="queryParams_PeiJian.pageSize"
            @pagination="getList_peijian"
          />

          <!-- 添加或修改配件信息对话框 -->
          <el-dialog :title="title_peijian" :visible.sync="open_peijian" width="500px" append-to-body>
            <el-form ref="form_peijian" :model="form_peijian" :rules="rules_peijian" label-width="80px">
              <el-form-item label="配件编号" prop="accessoryId">
                <el-input v-model="form_peijian.accessoryId" placeholder="配件编号后台自动生成" disabled/>
              </el-form-item>
              <el-form-item label="配件名称" prop="accessoryName">
                <el-input v-model="form_peijian.accessoryName" placeholder="请输入配件名称"/>
              </el-form-item>
              <el-form-item label="入库量" prop="stockInput">
                <el-input v-model="form_peijian.stockInput" placeholder="请输入入库量"/>
              </el-form-item>
              <el-form-item label="出库量" prop="stockOutput">
                <el-input v-model="form_peijian.stockOutput" placeholder="请输入出库量"/>
              </el-form-item>
              <!--              <el-form-item label="库存量" prop="stockQuantity">-->
              <!--                <el-input v-model="form_peijian.stockQuantity" placeholder="请输入库存量" />-->
              <!--              </el-form-item>-->
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm_peijian">确 定</el-button>
              <el-button @click="cancel_peijian">取 消</el-button>
            </div>
          </el-dialog>
        </el-card>
      </el-tab-pane>
      <el-tab-pane label="费用结算">
        <el-card>
          费用结算
        </el-card>
        <el-card>
          <el-form :model="querySpend" ref="querySpend" size="small" :inline="true" v-show="true" label-width="80px">
            <el-form-item label="客户编号" prop="customerId">
              <el-input style="width: 300px" v-model="querySpend.customerId" placeholder="请输入客户编号" readonly disabled/>
            </el-form-item>
            <el-form-item label="手机号" prop="phoneNum">
              <el-input style="width: 300px" v-model="querySpend.phoneNum" placeholder="根据手机号查询客户编号"/>
              <el-button type="primary" style="cursor: pointer" @click="queryCustomerIdByPhone">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery_Spend">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery_Spend">重置</el-button>
            </el-form-item>
          </el-form>
          <!--     费用结算表     -->
          <el-table v-loading="spendLoading" :data="spendList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="客户编号" align="center" prop="customerId"/>
            <el-table-column label="支付费用" align="center" prop="maintenanceCost"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-mobile-phone"
                  @click="spendPaid(scope.row)"
                  v-hasPermi="['system:accessory_info:edit']"
                >二维码支付
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total_feiyong>0"
            :total="total_feiyong"
            :page.sync="querySpend.pageNum"
            :limit.sync="querySpend.pageSize"
            @pagination="handleQuery_Spend"
          />
        </el-card>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {
  listAccessory_info,
  querySpend_List,
  getAccessory_info,
  delAccessory_info,
  addAccessory_info,
  updateAccessory_info
} from "@/api/4s_shop/accessory_info";

import {
  addMaintenance_order_info,
  addMaintenance_order_info_Batch,
  delMaintenance_order_info,
  getMaintenance_order_info,
  listMaintenance_order_info,
  updateMaintenance_order_info
} from "@/api/4s_shop/maintenance_order_info";
import {getAppointmentsList} from "@/api/4s_shop/customer_appointments";
import {
  addWork_order_info,
  delWork_order_info,
  addWork_order_info_batch,
  getWork_order_info,
  listWork_order_info,
  all_listWork_order_info,
  updateWork_order_info
} from "@/api/4s_shop/work_order_info";
import {getCustomerId} from "@/api/4s_shop/info";
import QRCode from 'qrcode'
import { ElMessageBox } from 'element-ui';
export default {
  name: "Maintenance_order_info",
  components: {
    QRCode,
  },
  data() {
    return {
      //批量生成维修单
      formWXD_List:[],
      //批量生成工作单
      formGZD_List:[],
      querySpend: {
        pageNum:1,
        pageSize:10,
      },
      total_feiyong:0,
      // 遮罩层
      loading_peijian: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single_peijian: true,
      // 非多个禁用
      multiple_peijian: true,
      // 显示搜索条件
      showSearch_peijian: true,
      // 总条数
      total_peijian: 0,
      // 配件信息表格数据
      accessory_infoList: [],
      // 弹出层标题
      title_peijian: "",
      // 是否显示弹出层
      open_peijian: false,
      // 查询参数
      queryParams_PeiJian: {
        pageNum: 1,
        pageSize: 7,
        accessoryId: null,
        accessoryName: null,
        stockInput: null,
        stockOutput: null,
        stockQuantity: null
      },
      // 表单参数
      form_peijian: {},
      // 表单校验
      rules_peijian: {
        // accessoryId: [
        //   { required: true, message: "配件编号不能为空", trigger: "blur" }
        // ],
        accessoryName: [
          {required: true, message: "配件名称不能为空", trigger: "blur"}
        ],
        stockInput: [
          {required: true, message: "入库量不能为空", trigger: "blur"}
        ],
        stockOutput: [
          {required: true, message: "出库量不能为空", trigger: "blur"}
        ],
        stockQuantity: [
          {required: true, message: "库存量不能为空", trigger: "blur"}
        ]
      },
      pageSizes: [7,10],
      repairStatusOptions: [{
        value: '0',
        label: '待处理'
      }, {
        value: '1',
        label: '进行中'
      }, {
        value: '2',
        label: '已完成'
      }, {
        value: '3',
        label: '已取消'
      },],
      // 遮罩层
      loading_GZD: true,
      // 选中数组
      ids_peijian: [],
      // 非单个禁用
      singleGZD: true,
      // 非多个禁用
      multipleGZD: true,
      // 显示搜索条件
      showSearch_GZD: true,
      // 总条数
      totalGZD: 0,
      // 工作单信息表格数据
      work_order_infoList: [],
      // 弹出层标题
      titleGZD: "",
      // 是否显示弹出层
      openGZD: false,
      // 查询参数
      queryParams_GZD: {
        pageNum: 1,
        pageSize: 7,
        workOrderId: null,
        customerId: null,
        vehicleEntryTime: null,
        estimatedCompletionTime: null,
        // 1是维修
        repairType: '1',
        repairContent: null,
        repairStatus: null
      },
      // 表单参数
      formGZD: {},
      // 表单校验
      rulesGZD: {
        workOrderId: [
          {required: true, message: "工单编号不能为空", trigger: "blur"}
        ],
        customerId: [
          {required: true, message: "客户编号不能为空", trigger: "blur"}
        ],
        vehicleEntryTime: [
          {required: true, message: "车辆入厂时间不能为空", trigger: "blur"}
        ],
        estimatedCompletionTime: [
          {required: true, message: "预计完成时间不能为空", trigger: "blur"}
        ],
        repairType: [
          {required: true, message: "类型不能为空", trigger: "change"}
        ],
        repairContent: [
          {required: true, message: "维修内容不能为空", trigger: "blur"}
        ],
        repairStatus: [
          {required: true, message: "维修状态不能为空", trigger: "change"}
        ]
      },
      isPaidOptions: [
        {
          value: '0',
          label: '未支付'
        },
        {
          value: '1',
          label: '已支付'
        },
      ],
      tabPosition: 'left',
      // 遮罩层
      loading: true,
      // 选中数组
      idsGZD: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中数组
      idsSpend: [],
      // 非单个禁用
      singleSpend: true,
      // 非多个禁用
      multipleSpend: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修单信息表格数据
      maintenance_order_infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 7,
        maintenanceOrderId: null,
        workOrderId: null,
        customerId: null,
        sparePartsId: null,
        maintenanceCost: null,
        isPaid: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      customer_appointmentsList: [],
      showRepairStatus: {
        "0": "待处理",
        "1": "进行中",
        "2": "已完成",
        "3": "已取消",
      },
      serviceTypeTableShow: {
        "0": "保养",
        "1": "维修",
        "2": "检测"
      },
      showIsPaid: {
        "0": "未支付",
        "1": "已支付"
      },
      spendLoading: true,
      spendList: [],
      // 二维码信息
      visible: false, // 控制弹出框是否显示
      qrValue: 'your-digit', // 这里应该是你想要生成二维码的数字
      qrOptions: { // 二维码的配置选项
        width: 200,
        height: 200,
      },
    };
  },
  created() {
    // 先根据预约信息整理表单（查询未取消的预约信息）,查出后将新表单插入工作单信息表
    this.getAppointmentsList()


    this.getList_peijian()
    this.handleQuery_Spend()
  },
  methods: {
    // 获取预约信息，生成工作单
    getAppointmentsList() {
      getAppointmentsList().then(response => {
        this.customer_appointmentsList = response.rows;
        console.log("未取消的预约单",this.customer_appointmentsList)
        // console.log('预约信息', this.customer_appointmentsList)
        for (let i = 0; i < this.customer_appointmentsList.length; i++) {
          var item = this.customer_appointmentsList[i]
          var formGZD = {}
          formGZD.workOrderId = null//自动生成
          formGZD.customerId = item.customerId
          formGZD.vehicleEntryTime = item.appointmentTime //入场时间即预约时间
          var date = new Date(item.appointmentTime)
          date.setDate(date.getDate() + 3)
          formGZD.estimatedCompletionTime = date.toISOString().slice(0, 10) //加上三天
          formGZD.repairType = item.serviceType
          formGZD.repairContent = "根据具体情况而定"
          formGZD.repairStatus = "0"  //0表示待处理
          // console.log(this.formGZD)
          // 后将新表单插入维修单信息表    限制客户编号  一个客户只能有一个预约信息   所以对应工单也只能有一个
          // this.submitFormGZD()
          this.formGZD_List.push(formGZD)
        }
        // console.log("待插入",this.formGZD_List)
        this.submitFormGZD_Batch()
      });
    },
    /** 查询工作单单信息列表 整合信息  插入维修单*/
    getAllGZDinfoForWXD(){
      all_listWork_order_info().then(response => {
        var work_order_infoList=[];
        work_order_infoList = response.rows;
        // console.log('工作单列表',this.work_order_infoList)
        for (let i = 0; i < work_order_infoList.length; i++) {
          var item = work_order_infoList[i]
          var form = {}
          form.maintenanceOrderId = null  //后台生成  并与workOrderId保持一致，这里属于开发事故,没设计好数据库，用chatgpt来设计试一试
          form.workOrderId = item.workOrderId
          form.customerId = item.customerId
          form.sparePartsId = "暂未更新"  //零件编号，
          form.maintenanceCost = "暂未更新" //维修花费，手动输入
          form.isPaid = "0"  // 0代表未支付
          this.formWXD_List.push(form)
        }
        console.log("待插入维修单的工作单信息",this.formWXD_List)
        //批量插入维修单
        this.submitWXDFormList()
        // this.getList()

      });
    },

    getGZDinfoForWXD() {
      listWork_order_info(this.queryParams_GZD).then(response => {
        this.work_order_infoList = response.rows;
        // console.log('工作单列表',this.work_order_infoList)
        for (let i = 0; i < this.work_order_infoList.length; i++) {
          var item = this.work_order_infoList[i]
          this.form.maintenanceOrderId = null  //后台生成  并与workOrderId保持一致，这里属于开发事故,没设计好数据库，用chatgpt来设计试一试
          this.form.workOrderId = item.workOrderId
          this.form.customerId = item.customerId
          this.form.sparePartsId = "暂未更新"  //零件编号，
          this.form.maintenanceCost = "暂未更新" //维修花费，手动输入
          this.form.isPaid = "0"  // 0代表未支付
          this.submitForm()
        }
        this.getList()
        this.$modal.msgSuccess("维修单信息已更新")
      });
    },
    /** 查询维修单信息列表 */
    getList() {
      this.loading = true;
      listMaintenance_order_info(this.queryParams).then(response => {
        this.total = response.total;
        // console.log('查询维修单的输出',this.total )
        this.maintenance_order_infoList = response.rows;
        for (let i = 0; i < this.maintenance_order_infoList.length; i++) {
          var item = this.maintenance_order_infoList[i]
          item.repairStatus = '请前往工单管理查询'
          item.isPaid = this.showIsPaid[item.isPaid]
        }
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        maintenanceOrderId: null,
        workOrderId: null,
        customerId: null,
        sparePartsId: null,
        maintenanceCost: null,
        isPaid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.maintenanceOrderId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleSelectionChangeSpend(selection) {
      this.idsSpend = selection.map(item => item.customerId)
      this.singleSpend = selection.length !== 1
      this.multipleSpend = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const maintenanceOrderId = row.maintenanceOrderId || this.ids
      getMaintenance_order_info(maintenanceOrderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改维修单信息";
      });
    },
    /** 提交维修单信息按钮 */
    submitForm() {
      if (this.form.maintenanceOrderId != null) {
        updateMaintenance_order_info(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else {
        addMaintenance_order_info(this.form).then(response => {
          // this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //
      //   }
      // });
    },
    //批量提交维修单
    submitWXDFormList(){
      addMaintenance_order_info_Batch(this.formWXD_List).then(response => {
        // this.$modal.msgSuccess("新增成功");
        this.getList();
        this.$modal.msgSuccess("维修单信息已更新")
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const maintenanceOrderIds = row.maintenanceOrderId || this.ids;
      this.$modal.confirm('是否确认删除维修单信息编号为"' + maintenanceOrderIds + '"的数据项？').then(function () {
        return delMaintenance_order_info(maintenanceOrderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/maintenance_order_info/export', {
        ...this.queryParams
      }, `maintenance_order_info_${new Date().getTime()}.xlsx`)
    },


    /** 查询工作单信息列表 */
    getListGZD() {
      this.loading_GZD = true;
      listWork_order_info(this.queryParams_GZD).then(response => {
        this.work_order_infoList = response.rows;
        for (let i = 0; i < this.work_order_infoList.length; i++) {
          var item = this.work_order_infoList[i]
          item.repairStatus = this.showRepairStatus[item.repairStatus]
          item.repairType = this.serviceTypeTableShow[item.repairType]
        }
        this.totalGZD = response.total;
        this.loading_GZD = false;
      });
    },
    // 取消按钮
    cancelGZD() {
      this.openGZD = false;
      this.resetGZD();
    },
    // 表单重置
    resetGZD() {
      this.formGZD = {
        workOrderId: null,
        customerId: null,
        vehicleEntryTime: null,
        estimatedCompletionTime: null,
        repairType: '1',
        repairContent: null,
        repairStatus: null
      };
      this.resetForm("formGZD");
    },
    /** 搜索按钮操作 */
    handleQuery_GZD() {
      this.queryParams_GZD.pageNum = 1;
      this.getListGZD();
    },
    /** 重置按钮操作 */
    resetQuery_GZD() {
      this.resetForm("queryForm_GZD");
      this.handleQuery_GZD();
    },
    // 多选框选中数据
    handleSelectionChangeGZD(selection) {
      this.ids = selection.map(item => item.workOrderId)
      this.singleGZD = selection.length !== 1
      this.multipleGZD = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd_GZD() {
      this.resetGZD();
      this.openGZD = true;
      this.titleGZD = "添加工作单信息";
    },
    /** 修改按钮操作 */
    handleUpdate_GZD(row) {
      // this.resetGZD();
      this.formGZD.workOrderId = row.workOrderId
      const workOrderId = row.workOrderId
      getWork_order_info(workOrderId).then(response => {
        this.form = response.data;
        this.openGZD = true;
        this.titleGZD = "修改工作单信息";
      });
    },
    // 批量生成工作单
    submitFormGZD_Batch(){
      addWork_order_info_batch(this.formGZD_List).then(response => {
        // 查询显示工作单
        this.getListGZD();
        this.$modal.msgSuccess("工作单信息已更新");
        // 再根据工作单信息表，整理数据，插入维修单信息表
        this.getAllGZDinfoForWXD()
      });
    },
    submitFormGZD() {
      if (this.formGZD.workOrderId != null) {
        updateWork_order_info(this.formGZD).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.openGZD = false;
          this.getListGZD();
        });
      } else {
        addWork_order_info(this.formGZD).then(response => {
          // this.openGZD = false;
          // this.getListGZD();
        });
      }
      // this.$refs["formGZD"].validate(valid => {
      //   if (valid) {
      //
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete_GZD(row) {
      const workOrderIds = row.workOrderId || this.ids;
      this.$modal.confirm('是否确认删除工作单信息编号为"' + workOrderIds + '"的数据项？').then(function () {
        return delWork_order_info(workOrderIds);
      }).then(() => {
        this.getListGZD();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport_GZD() {
      this.download('system/work_order_info/export', {
        ...this.queryParams_GZD
      }, `work_order_info_${new Date().getTime()}.xlsx`)
    },

    //配件管理
    /** 查询配件信息列表 */
    getList_peijian() {
      this.loading_peijian = true;
      listAccessory_info(this.queryParams_PeiJian).then(response => {
        this.accessory_infoList = response.rows;
        this.total_peijian = response.total;
        this.loading_peijian = false;
      });
    },
    // 取消按钮
    cancel_peijian() {
      this.open_peijian = false;
      this.reset_peijian();
    },
    // 表单重置
    reset_peijian() {
      this.form_peijian = {
        accessoryId: null,
        accessoryName: null,
        stockInput: null,
        stockOutput: null,
        stockQuantity: null
      };
      this.resetForm("form_peijian");
    },
    /** 搜索按钮操作 */
    handleQuery_peijian() {
      this.queryParams_PeiJian.pageNum = 1;
      this.getList_peijian();
    },
    /** 重置按钮操作 */
    resetQuery_peijian() {
      this.resetForm("queryForm_peijian");
      this.handleQuery_peijian();
    },
    // 多选框选中数据
    handleSelectionChange_peijian(selection) {
      this.ids = selection.map(item => item.accessoryId)
      this.single_peijian = selection.length !== 1
      this.multiple_peijian = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd_peijian() {
      this.reset_peijian();
      this.open_peijian = true;
      this.title_peijian = "添加配件信息";
    },
    /** 修改按钮操作 */
    handleUpdate_peijian(row) {
      this.reset_peijian();
      const accessoryId = row.accessoryId || this.ids
      getAccessory_info(accessoryId).then(response => {
        this.form_peijian = response.data;
        this.open_peijian = true;
        this.title_peijian = "修改配件信息";
      });
    },
    /** 提交按钮 */
    submitForm_peijian() {
      this.form_peijian.stockQuantity = parseInt(this.form_peijian.stockInput) - parseInt(this.form_peijian.stockOutput).toString()
      this.$refs["form_peijian"].validate(valid => {
        if (valid) {
          if (this.form_peijian.accessoryId != null) {
            updateAccessory_info(this.form_peijian).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open_peijian = false;
              this.getList_peijian();
            });
          } else {
            addAccessory_info(this.form_peijian).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open_peijian = false;
              this.getList_peijian();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete_peijian(row) {
      const accessoryIds = row.accessoryId || this.ids;
      this.$modal.confirm('是否确认删除配件信息编号为"' + accessoryIds + '"的数据项？').then(function () {
        return delAccessory_info(accessoryIds);
      }).then(() => {
        this.getList_peijian();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport_peijian() {
      this.download('system/accessory_info/export', {
        ...this.queryParams_PeiJian
      }, `accessory_info_${new Date().getTime()}.xlsx`)
    },
    // 根据手机号查询Id
    queryCustomerIdByPhone() {
      if (this.querySpend.phoneNum == undefined) {
        this.$modal.msgError('手机号不可为空!')
      } else {
        getCustomerId(this.querySpend.phoneNum).then(response => {
          this.$forceUpdate(this.querySpend.customerId = response)
        });
      }
    },
    // 根据客户编号查询费用，并生成付费二维码
    handleQuery_Spend() {
      listMaintenance_order_info(this.querySpend).then((res) => {
        // console.log(res)
        this.total_feiyong = res.total
        this.spendList = res.rows
        this.spendLoading = false
      })
    },
    resetQuery_Spend() {
      this.querySpend.customerId = undefined
      this.querySpend.phoneNum = undefined
    },
    // 支付
    isNumeric(value) {
      return !isNaN(value) && // 判断是否不是NaN
        value !== null && // 判断是否不是null
        typeof value !== 'undefined'; // 判断是否不是undefined
    },
    spendPaid(row) {
      console.log(row.maintenanceCost)

      // 假设要生成的数字是 12345
      var moneyNum = row.maintenanceCost
      var isNum = this.isNumeric(moneyNum)
      if(isNum){
        QRCode.toDataURL('you need to pay '+moneyNum.toString()+'$', (err, url) => {
          if (err) {
            console.error(err);
            return;
          }
          this.$confirm(`<img src="${url}" alt="QR Code"/>`,`微信支付二维码`,{
            dangerouslyUseHTMLString: true
          });
        });
      }else {
        this.$confirm("金额暂未更新，请稍后支付");
      }

    }
  }
};
</script>
